<template>
  <div>
    <el-dialog
      :title="title"
      @close="cancel"
      :visible.sync="dialogVisible"
      width="50%">
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="选择平台" prop="platformId"
                          v-if="dialogVisible">
              <el-select v-model="form.platformId"
                         value-key="platformId"
                         clearable placeholder="请选择平台">
                <el-option
                  v-for="item in gpsList"
                  :key="item.id"
                  :label="item.plaformName"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户名称" prop="userName">
              <el-input  v-model="form.userName" placeholder="请输入用户名称"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="密码" prop="gpsPassword">
              <el-input  v-model="form.gpsPassword" placeholder="请输入密码"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-input v-show="false" v-model="form.pic"></el-input>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {addGPS, updateGPS} from "@/api/merchant/car";

export default {
  name: "addPerson",
  created() {
    this.init()
  },
  props:['gpsList'],
  computed:{
    userId() {
      return this.$store.getters.userId
    },
  },
  data() {
    return {
      title: '新增门店',
      deviceOptions: [],
      previewDialogVisibleceshi: false,
      attachFilesceshi: [],
      previewImageListceshi: [],
      dialogVisible: false,
      shop_status: [],
      shop_ranges: [],
      citydata: [],
      rules: {
        plaformName: [
          {required: true, message: "平台名称不能为空", trigger: "blur"}
        ],
        domainName: [
          {required: true, message: "域名不能为空", trigger: "blur"}
        ],
        userName: [
          {required: true, message: "gps用户名不能为空", trigger: "blur"}
        ],
        gpsPassword: [
          {required: true, message: "gps密码不能为空", trigger: "blur"}
        ]
      },
      branchOptions: [],
      supplierUnitList: [],
      isCheck: '1',
      form: {
        platformId: null,
        sid: null,
        domainName: null,
        colour: null,
        gpsPassword: null,
        userName: null,
        status: null,
      }
    }
  },
  methods: {
    init() {
      // getGpsData({
      //   pageNum: 1,
      //   pageSize: 100,
      //   userId:1,
      // }).then(res => {
      //   if (res.code == 200) {
      //     this.branchOptions = res.rows
      //   }
      // })
      // 获取省市区三级
      // getCityList({pid: 0}).then(response => {
      //   this.citydata = response.data;
      // });
      // listVehsupplierunit({pageNum: 1, pageSize: 100}).then(response => {
      //   let info = response.rows
      //   this.supplierUnitList = []
      //   for (let i = 0; i < info.length; i++) {
      //     if (info[i].unitType == 0) {
      //       this.supplierUnitList.push({label: info[i].personName, value: info[i].id})
      //     } else {
      //       this.supplierUnitList.push({label: info[i].supplierUnitName, value: info[i].id})
      //     }
      //   }
      // });
    },
    submitForm() {
      // 获取图片及门店信息的值
      if (this.form.id) {
        updateGPS(this.form).then(res => {
          this.$message({
            message: '平台账号修改成功',
            type: 'success'
          });
          this.dialogVisible = false
          this.$emit('update')
        })
        this.form.userId=this.userId
        // editGpsInfo(this.form).then(res => {
        //   if (res.code == 200) {
        //     this.$modal.msgSuccess("修改成功");
        //     this.init()
        //   }
        // })
        // this.$emit('update')
      } else {
        addGPS(this.form).then(res => {
          this.$message({
            message: '平台账号添加成功',
            type: 'success'
          });
          this.dialogVisible = false
          //window.parent.location.reload()
          this.$emit('update')
        })

        this.form.userId=this.userId
        // editGpsInfo(this.form).then(res => {
        //   if (res.code == 200) {
        //     this.$modal.msgSuccess("修改成功");
        //     this.init()
        //   }
        // })
      }
    },
    open(info) {
      if (info) {
        this.form = info
        console.log('form', this.form)
        for (let i = 0; i < this.branchOptions.length; i++) {
          if (this.branchOptions[i].id==info.sid){
            this.$set(this.form,'storeObj',this.branchOptions[i])
            break
          }
        }
        this.title = '修改平台'
      } else {
        this.title = '新增平台'
        this.form = {
          areaCity: '',
          businessHours: '',
          shopTel: '',
          linkman: '',
          ranges: '',
          status: '',
          serviceTel: ''
        }
      }
      this.dialogVisible = true
    },
    cancel() {
      this.dialogVisible = false
    }
  }
}
</script>
<style scoped lang="scss">

</style>
